package com.brucekong.slm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Configuration
public class WebSecurityConfig extends WebMvcConfigurationSupport {

    /**
     * login session key
     */
    public static String SESSION_KEY = "slm_user";

    @Bean
    public SecurityInterceptor getSecurityInterceptor() {
        return new SecurityInterceptor();
    }

    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());
        // 拦截所有路径
        addInterceptor.addPathPatterns("/**"); // 除了/login  /error 静态资源，所有的 /** 都拦截住
        // exclude CustomConfig 排除设置
        addInterceptor.excludePathPatterns("/error");
        addInterceptor.excludePathPatterns("/login**");
        addInterceptor.excludePathPatterns("/register**");
        addInterceptor.excludePathPatterns("/lostwd**");
        addInterceptor.excludePathPatterns("/logout**");
        addInterceptor.excludePathPatterns("/user/judge");
        // addInterceptor.excludePathPatterns("/static/**");
        // static 已经映射到 / 目录下了
        addInterceptor.excludePathPatterns("/img/**");
        addInterceptor.excludePathPatterns("/js/**");
        addInterceptor.excludePathPatterns("/css/**");
        // addInterceptor.excludePathPatterns("/admin/**");
        addInterceptor.excludePathPatterns("/libs/**");

    }

    public class SecurityInterceptor extends HandlerInterceptorAdapter {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            HttpSession session = request.getSession(true);
            if (null != session.getAttribute(SESSION_KEY)) {
                // 登录了，就继续
                session.setAttribute(SESSION_KEY, session.getAttribute(SESSION_KEY));
                return true;
            }
            // 重定向到登录界面
            response.sendRedirect("/login");
            return false;
        }
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将 static 映射到 / 路径下
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * Supports FileUploads.
     */
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();

        multipartResolver.setDefaultEncoding("UTF-8");
        //resolveLazily属性启用是为了推迟文件解析，以在在UploadAction中捕获文件大小异常
        multipartResolver.setResolveLazily(true);
        multipartResolver.setMaxInMemorySize(40960);
        //上传文件大小 50M 50*1024*1024
        multipartResolver.setMaxUploadSize(50 * 1024 * 1024);

        return multipartResolver;
    }
}
